package com.zmht.beike.biz.impl;

import java.io.Serializable;
import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.zmht.beike.biz.HabitBiz;
import com.zmht.beike.core.BaseBizImpl;
import com.zmht.beike.po.THabit;

public class HabitBizImpl extends BaseBizImpl implements HabitBiz {

	public THabit findById(Serializable id) {
		return (THabit)this.getHabitDAO().findById(THabit.class, id);
		}

	public THabit findByStudentId(Serializable studentId) {
		String hql = "from THabit t where t.studentId="+studentId+" order by t.id desc";
		List<THabit> list = this.getHabitDAO().findByHQLObj(hql);
		if(list!=null&&list.size()>0){return list.get(0);}
		return null;
	}

	public boolean saveHabit(THabit habit) {
		try{
			this.getHabitDAO().saveObj(habit);
		}catch(Exception e){
			return false;
		}
		return true;
	}

	public boolean updateHabit(THabit habit) {
		try{
			this.getHabitDAO().updateObj(habit);
		}catch(Exception e){
			return false;
		}
		return true;
	}
	public static void main(String [] args){
		ApplicationContext cxt;
		cxt = new ClassPathXmlApplicationContext(new String[]{"spring/spring.xml","spring/spring_dao.xml","spring/spring_beike.xml"});
		HabitBizImpl biz = (HabitBizImpl)cxt.getBean("habitBizTag");
		
		THabit hbt = new THabit();
		hbt.setStudentId(3);
		hbt.setCherish("aaaaa");
		biz.saveHabit(hbt);
		
		//THabit hbt = biz.findById(1);
		//System.out.println(hbt.getCherish());*/
		
	}

	public List<THabit> findByStudent(Serializable studentId,String startTime,String endTime) {
		String hql = "from THabit t where t.studentId="+studentId;
		if(!"".equals(startTime)){hql += " and t.ctime>='"+startTime+"' ";}
		if(!"".equals(endTime)){hql += " and t.ctime<='"+endTime+"' ";}
		hql += " order by t.id desc";
		List<THabit> list = this.getHabitDAO().findByHQLObj(hql);
		return list;
	}
}
